package kg.aibek.dc;

public class Utility {

	public static int bitStringToInt(String str) {
		int ret = 0;

		for (int i = 0; i < str.length(); i++) {
			ret += Math.pow(2, i) * (new Integer(str.charAt(str.length() - i - 1) + ""));
		}

		return ret;
	}

	public static int getBitAtPosition(int value, int pos) {
		int ret = new Integer(getBitString(value).charAt(pos) + "");

		return ret;
	}

	public static String getBitString(int value) {
		String retStr = Integer.toBinaryString(value);

		if (retStr.length() > AdaptiveModel.BIT_LENGTH) {
			retStr = retStr.substring(retStr.length() - AdaptiveModel.BIT_LENGTH);
		} else if (retStr.length() < AdaptiveModel.BIT_LENGTH) {
			String tmp = "";
			for (int i = 0; i < AdaptiveModel.BIT_LENGTH - retStr.length(); i++) {
				tmp += "0";
			}
			retStr = tmp + retStr;
		}

		return retStr;
	}

	// Shifts the bitString to left and appends lsb at the end
	public static int shiftMSB(int value, int lsb) {
		String str = getBitString(value);
		str = str.substring(1);
		str += lsb;

		int ret = bitStringToInt(str);
		return ret;
	}

	public static int complementMSB(int value) {
		String str = getBitString(value);
		str = ((getBitAtPosition(value, 0) + 1) % 2) + str.substring(1);
		return bitStringToInt(str);
	}

}
